﻿using CarEdge.Infrastructure.Dto.Request.Consume;
using CarEdge.Infrastructure.Dto.Request.Customer;
using CarEdge.Service.Instance;
using CarEdge.Service.Interface;
using CarEdge.Service.Interface.Customer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CarEdge.Admin.Controllers.customer
{
    /// <summary>
    /// 客户管理
    /// </summary>
    /// 
    [Authorize(Roles = "Admin")]
    public class customerController : BaseController
    {
        private readonly I_Customer_Service _Customer_Service;
        private readonly IConfiguration _configuration;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="login_Service"></param>
        /// <param name="i_Customer_Service"></param>
        /// <param name="configuration"></param>
        public customerController(I_Login_Service login_Service, I_Customer_Service i_Customer_Service, IConfiguration configuration) : base(login_Service)
        {
            _Customer_Service = i_Customer_Service;
            _configuration = configuration;
        }
        /// <summary>
        /// 查询所有客户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAllCustomers(int? page = 1, int? limit = 10, string? key = "")
        {
            var result = await _Customer_Service.GetAllCustomers(page ?? 1, limit ?? 10, key ?? "");
            return Ok(result);
        }
        /// <summary>
        /// 添加客户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddCustomer(Customer_Request_Dto dto)
        {
            var user = await this.Get_Curr_User();
            dto.author = user.Uid;
            dto.responsibility=string.IsNullOrEmpty(dto.responsibility)?user.Uid:dto.responsibility;
            var result = await _Customer_Service.AddCustomer(dto);
            return Ok(result);
        }

        /// <summary>
        /// 修改客户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateCustomer(Customer_Request_Dto dto)
        {
            var result = await _Customer_Service.UpdateCustomer(dto);
            return Ok(result);
        }
        /// <summary>
        /// 通过id查找客户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetCustomerById(string id)
        {
            var result = await _Customer_Service.GetCustomerById(id);
            return Ok(result);
        }

        /// <summary>
        /// 通过id查询客户所有跟进
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetCustomerRecordsById(string id)
        {

            var result = await _Customer_Service.GetCustomerRecordsById(id);
            return Ok(result);
        }
        /// <summary>
        /// 添加用户跟进
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddCustomerRecord(Customer_Record_Request_Dto dto)
        {
            var user = await this.Get_Curr_User();
            dto.author = user.Uid;
            var result = await _Customer_Service.AddCustomerRecord(dto);
            return Ok(result);
        }
     
      
        /// <summary>
        /// 新增会员
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Addvip(Customer_Request_Dto dto)
        {
            var user= await this.Get_Curr_User();
            dto.author = user.Uid;
            var result = await _Customer_Service.Addvip(dto);
            return Ok(result);
        }
        /// <summary>
        /// 获取所有的员工
        /// </summary>
        /// <returns></returns>
        [HttpGet]
       public async Task<IActionResult> GetAllUsers(string? key)
        {

            var result=await _Customer_Service.GetAllusers(key??"");
            return Ok(result);
        }
        /// <summary>
        /// 充值
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Recharge(Consume_Request_Dto dto)
        {
            var result = await _Customer_Service.Recharge(dto);
            return Ok(result);
        }

        /// <summary>
        /// 消费
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Consumption(Consume_Request_Dto dto)
        {
            var result = await _Customer_Service.Consumption(dto);
            return Ok(result);
        }
    }
}
